package cn.wolfcode.wolf2w.service.imp;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.query.RegionQuery;
import cn.wolfcode.wolf2w.repository.RegionRepository;
import cn.wolfcode.wolf2w.service.IRegionService;

import com.sun.org.apache.bcel.internal.generic.RETURN;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
//@Transactional  暂时先别管事务
public class RegionServiceImpl implements IRegionService {

    @Autowired
    private RegionRepository repository;

    @Autowired
    private MongoTemplate template;


    @Override
    public Region get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public void update(Region region) {
        repository.save(region);
    }

    //page对象里面包括: 总页数 每页显示条数......
    public Page<Region> query(RegionQuery qo) {
        //查询对象
        Query query = new Query();

        Long totalCount = template.count(query, Region.class);
        if (totalCount == 0) {
            return Page.empty();//空的分页对象
        }
        //list : 当前页面显示数据集合
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        query.with(pageable);
        List<Region> list = template.find(query, Region.class);

        return new PageImpl<>(list, pageable, totalCount);
    }

    public void saveOrUpdate(Region region) {
        if (!StringUtils.hasLength(region.getId())) {
            //添加
            region.setId(null);
            repository.save(region);
        } else {
            //更新
            //方案1: 1:查询  2: 替代  3:save
            //方案2: 显示使用update逻辑 update set...
            //条件
            Query query = new Query();
            query.addCriteria(Criteria.where("_id").is(region.getId()));
           /*
            //更新的列
            Update update = new Update();
            update.set("name",region.getName());
            update.set("sn",region.getSn());
            update.set("refIds",region.getRefIds());
            update.set("ishot",region.getIshot());
            update.set("sequence",region.getSequence());
            update.set("info",region.getInfo());
            //执行更新
            template.updateMulti(query, update, Region.class);*/
            // DBHelper.update(template, query, region, "name","sn","refIds","ishot","sequence","info");

        }
    }

    @Override
    public void changeHotValue(int hot, String id) {
        Region regin = this.get(id);


        regin.setIshot(hot);
        this.update(regin);
    }


    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public List<Region> queryHotRegion() {

        return repository.findByIshotOrderBySequenceAsc(Region.STATE_HOT);
    }

    @Override
    public List<Region> list() {
        return repository.findAll();
    }


}
